// pages/admin/manage/user/index.js
const cloudHelper = require('../../../../helper/cloud_helper.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isFormShow: false,
        isListLoading: true,
        form: {
            _id: '',
            props: {
                user_nickname: {
                    name: 'user_nickname',
                    value: '',
                    label: "姓名",
                    type: 'input'
                },
                user_phone: {
                    name: 'user_phone',
                    value: '',
                    label: "手机号",
                    type: 'input'
                },
                user_is_admin: {
                    name: 'user_is_admin',
                    value: '',
                    label: "管理员",
                    type: 'radio',
                    options: [{
                        label: "是",
                        value: "1"
                    },
                    {
                        label: "否",
                        value: "0"
                    },
                ]
                }
            }
        },
        formDefault: {},
        data: {
            count: 0,
            list: [],
            page: 1,
            size: 20,
            total: 0,
        },
        keyword: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            formDefault: JSON.parse(JSON.stringify(this.data.form))
        });
        this.getList();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('触底了');
        if (this.data.data.count > this.data.data.page) {
            this.getList(false);
        }

    },

    // 点击查询
    onSearch() {
        this.getList();
    },

    // 点击新增
    onAdd() {
        this.setData({
            isFormShow: true,
            form: JSON.parse(JSON.stringify(this.data.formDefault))
        });
    },

    // 点击编辑
    onEdit(e) {
        let item = e.currentTarget.dataset.item;
        let form = this.data.form;
        form._id = item._id;
        form.props.user_nickname.value = item.user_nickname;
        form.props.user_phone.value = item.user_phone;
        form.props.user_is_admin.value = item.user_is_admin || 0;
        this.setData({
            isFormShow: true,
            form
        })
    },

    // 点击更多
    onMore(e) {
        let that = this;
        wx.showActionSheet({
            itemList: ['编辑'],
            success(res) {
                if (res.tapIndex == 0) {
                    that.onEdit(e);
                }
            },
            fail(res) {}
        })
    },

    // 点击表单确定按钮
    onSubmitForm(e) {
        let formData = e.detail;
        let params = {};
        params._id = formData._id;
        params.user_nickname = formData.props.user_nickname.value;
        params.user_phone = formData.props.user_phone.value;
        params.user_is_admin = formData.props.user_is_admin.value;
        this.save(params);
    },

    // 获取列表
    async getList(isRefresh = true) {
        this.setData({
            isListLoading: true
        });
        let params = {
            keyword: this.data.keyword,
            page: this.data.data.page,
            size: this.data.data.size
        };
        if (isRefresh) {
            params.page = 1;
        } else {
            params.page++;
        }
        let res = await cloudHelper.callCloudData('api/admin/user_list', params);
        if (res && res.size) {
            if (isRefresh) {
                this.setData({
                    data: {
                        count: res.count,
                        list: res.list,
                        page: res.page,
                        size: res.size,
                        total: res.total
                    },
                })
            } else {
                let newList = this.data.data.list;
                newList.push(...res.list);
                this.setData({
                    data: {
                        count: res.count,
                        list: newList,
                        page: res.page,
                        size: res.size,
                        total: res.total
                    },
                })
            }
        }
        this.setData({
            isListLoading: false
        });
    },

    // 保存
    async save(params) {
        wx.showLoading();
        let res = await cloudHelper.callCloudData('api/admin/user_save', params);
        wx.hideLoading();
        if (res && res.res == true) {
            wx.showToast({
                title: '保存成功',
            })
            this.setData({
                isFormShow: false,
            })
            this.getList();
        }
    },

})